package com.regentsoft.cloudback.enterpriseAdmin.service.impl;

import com.regentsoft.cloudback.enterpriseAdmin.dao.LoginDao;
import com.regentsoft.cloudback.enterpriseAdmin.entity.CompanyAdmin;
import com.regentsoft.cloudback.enterpriseAdmin.service.CompanyLoginService;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.CompanyLoginReq;
import com.regentsoft.cloudback.exception.LoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by work on 2017/11/27.
 *
 * @author lumr
 */
@Service
public class CompanyLoginServiceImpl implements CompanyLoginService {
    @Autowired
    private LoginDao dao;

    @Override
    public CompanyAdmin login(CompanyLoginReq loginReq) {
        String name = dao.queryCompany(loginReq.getCompanyId());
        if (name == null)
            throw new LoginException("企业不存在");

        CompanyAdmin admin = dao.queryAdmin(loginReq.getCompanyId(), loginReq.getAccount());

        if (admin == null)
            throw new LoginException("用户不存在");

        if (admin.getPassword().equals(loginReq.getPassword())) {
            admin.setCompanyId(loginReq.getCompanyId());
            admin.setCompanyName(name);
            return admin;
        } else {
            throw new LoginException("密码错误");
        }
    }
}
